# @Time    : 2025/02/05
# @Author  : 斯奎匝德(张仪伟)
# @File    : SubmitProcessor.py
# @Software: PyCharm
#
# 上传内容处理
#
import json
from Utils import NewUUID,GetTimeNow
import DatabaseHelper
from flask import request, Blueprint, jsonify

SubmitProcessor = Blueprint('SubmitProcessor', __name__)

@SubmitProcessor.route("/api/submit",strict_slashes=False,methods=["POST"])
def submit():
    jsonData = request.get_data().decode()
    jsonData = json.loads(str(jsonData))
    Title = jsonData["title"]
    Content = jsonData["content"]
    #   注意，tags直接保存到数据库，在后续处理时进行split()
    # try:
    #     Tags = jsonData["tags"].split()
    # except ValueError:
    #     Tags = jsonData["tags"]
    ArchiveUUID = NewUUID()
    Comments = "{}" #   评论默认没有
    Tags = jsonData["tags"]
    Category = jsonData["category"]

    try:
        Result = DatabaseHelper.InsertArchive(ArchiveUUID, Title, Content,Comments,Category,Tags,GetTimeNow(True))
    except Exception:
        return jsonify({'error_code': 1, 'message': '寄了，数据插入失败'}),500
    #   what to insert?
    #   uuid, title, content, comments, category, tags, time
    if Result != True:
        return jsonify({'error_code': 1, 'message': f'寄！{Result}'}),500
    else:
        return jsonify({'error_code': 0, 'message': 'success'}),200